############################################################
# Dynamic DiD with Covariates: All Visas and Crime Concerns
############################################################

rm(list=ls())

library(Hmisc)
library(readstata13)
library(foreign)
library(tidyverse)
library(did)
library(ggplot2)
set.seed(1111)

#sink("02_results_concerns_all_covariates.txt")

############################
# Read and prepare data 
############################

# load data
load("final_county_2023april6.RData")
names(d_county)

#######################
# Results 
#######################

# d-did + covariates  
out2 <- att_gt(yname = "crime_concerns_s",
               gname = "first.treat_all",
               idname = "county",
               tname = "survey",
               xformla = ~turnout_2013 + income_2003 + population_2011 + right_voteshare_2013,
               data = d_county,
)

es2 <- aggte(out2, type = "dynamic",na.rm = TRUE)
summary(es2)
ggdid(es2)

# save results for overall effect (Table A17)
pe2 <- es2$overall.att
se2 <- es2$overall.se
results2 <- rbind(pe2,se2) 
results2
write.table(results2, "overallATT_2.txt", sep="\t")

# table a18
sink(file = "tableA18.txt")
summary(es2)
sink(file = NULL)

sink()
